package com.bruce.starts.demo.okhttp;


import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientDemo {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientDemo.class);
    private static final String BASE_URL = "https://a.szzhijing.com/woven/cloth/greyRoll/saveNew";
    private static final String clientId = "ED911F17065848D98D1EE7524F5526F2";
    private static final String accessToken = "0E922FD658634ECCBB7D98021F30E9CA";
    private static final String userId = "4C9E9C7FA8434381908314FA6103018E";
    private static final String orgCode = "92368E147E044587B9E404A89F892C95";

    public List<String> getPostJsonDatList() {
        String filePath = "D:\\07_致景科技\\01_进销存\\02_其他\\json.txt";
        List<String> result = new ArrayList<>();
        List<String> data = FileUtil.readLines(new File(filePath),"UTF-8");
        StringBuilder stringBuilder = new StringBuilder();
        data.forEach(str->{
            if(str.contains("NO====")){
                result.add(stringBuilder.toString().trim());
                stringBuilder.delete(0,stringBuilder.length());
            }else {
                stringBuilder.append(str);
            }
        });
        //result.forEach(str-> System.out.println(str));
        return result;
    }

    public void sendPost() throws Exception {
        OkHttpClient client = new OkHttpClient();
        List<String> jsonList = getPostJsonDatList();
        //List<String> jsonList = List.of(jsonData.get(0));
        jsonList.forEach(json -> {
            RequestBody body = RequestBody.create("["+json+"]",MediaType.parse("application/json"));
            Request request = new Request.Builder()
                    .url(BASE_URL)
                    .addHeader("clientId",clientId)
                    .addHeader("accessToken",accessToken)
                    .addHeader("userId",userId)
                    .addHeader("orgCode",orgCode)
                    .addHeader("Content-Type","application/json")
                    .post(body)
                    .build();

            try {
                Response response = client.newCall(request).execute();
                String result = response.body().string();
                logger.info(result);
                Boolean resultCode = (Boolean) JSONPath.read(result,"$.successful");
                if(!resultCode){
                    logger.info("布票【{}】保存失败！",JSONPath.extract(json, "$.barCode"));
                }else {
                    logger.info("布票【{}】保存成功！",JSONPath.extract(json, "$.barCode"));
                }
            } catch (IOException e) {
                e.printStackTrace();//响应失败了，进行响应操作
                logger.error("布票【{}】保存失败！",JSONPath.extract(json, "$.barCode"));
            }
            try {
                logger.info("------------------------------------------------------------------------");
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public static void main(String[] args) throws Exception {
        HttpClientDemo demo = new HttpClientDemo();
        demo.sendPost();
    }
}
